{% extends 'base.html' %}
{% from 'bootstrap5/pagination.html' import render_pagination %}

{% block title %}Manage Posts{% endblock %}

{% block content %}
    <div class="page-header">
        <h1>Posts &nbsp;<span class="text-muted">{{ pagination.total }}</span></h1>
        <a class="btn btn-secondary" style="float:right;" href="{{ url_for('admin.new_post') }}">New Post</a>
    </div>
    <div class="page-footer">
    {% if posts %}
        <table class="table table-secondary table-striped">
            <thead>
            <tr>
                <th>No</th>
                <th>Title</th>
                <th>Category</th>
                <th>Date</th>
                <th>Comments</th>
                <th>Words</th>
                <th>Actions</th>
            </tr>
            </thead>
            {% for post in posts %}
                <tr>
                    <td>
                        {{ (page - 1)*config['BLOG_MANAGE_POST_PER_PAGE'] + loop.index }}
                    </td>
                    <td>
                        <a href="{{ url_for('blog.show_post', post_id=post.id) }}">{{ post.title }}</a>
                    </td>
                    <td>
                        <a href="{{ url_for('blog.show_category', category_id=post.category.id) }}">{{ post.category.name }}</a>
                    </td>
                    <td>
                        {{ moment(post.timestamp).format('LL') }}
                    </td>
                    <td>
                        <a href="{{ url_for('blog.show_post', post_id=post.id) }}#comments">{{ post.comments | length }}</a>
                    </td>
                    <td>
                        {{ post.body | length }}
                    </td>
                    <td>
                        <a class="btn btn-success btn-sm" href="{{ url_for('admin.edit_post', post_id=post.id) }}">
                            Edit
                        </a>
                        <form class="inline" method="post" action="{{ url_for('.delete_post', post_id=post.id) }}">
                            <input type="hidden" name="csrf_token" value="{{ csrf_token() }}"/>
                            <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Are you sure?');">
                                Delete
                            </button>
                        </form>
                    </td>
                </tr>
            {% endfor %}
        </table>
        {{ render_pagination(pagination) }}
    {% else %}
        <div class="card">
            <div class="card-body tip"><h5>No Posts.</h5></div>
        </div>
    {% endif %}
    </div>
{% endblock %}
